package com.mlegrand.firestarter.commands.login
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.mlegrand.firestarter.business.SampleLoginDelegate;
	import com.mlegrand.firestarter.events.SampleLoginEvent;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	

	public class SampleLoginCommand implements ICommand, IResponder
	{
		public function SampleLoginCommand(){}

		protected var onFailureCallBack:Function;
		protected var onSuccessCallBack:Function;
		
		public function execute(event:CairngormEvent):void
		{
			var sampleLoginEvent:SampleLoginEvent = event as SampleLoginEvent;
			onFailureCallBack = sampleLoginEvent.failureCallBack
			onSuccessCallBack = sampleLoginEvent.successCallBack;
			var sampleLoginDelegate:SampleLoginDelegate = new SampleLoginDelegate(this);
			sampleLoginDelegate.verifyCredentials(sampleLoginEvent.firstName, sampleLoginEvent.password);
		}
		
		/**
		 * Handles the result.  
		 * I'd recommend putting it in a manager 
		 * class that is tied to the data model.
		 * Data is often a ResultEvent
		 * 
		 * @param		data		Object 
		 */ 
		public function result(data:Object):void 
		{
			if(data is ResultEvent)
			{
				if ( data.result == 1 ) 
				{
					trace('map your result to a public property in a manager class.')
					onSuccessCallBack();
				}
				else
				{
					onFailureCallBack();
				}
			}
		}
		
		/**
		 * Handles the fault event. 
		 * The information passed to the 
		 * fault method is usually a 
		 * Fault event but not always. 
		 * 
		 * @param		info		 Object
		 */ 
		public function fault(info:Object):void 
		{
			if(info is FaultEvent)
			{
				var fault:FaultEvent = info as FaultEvent;
				Alert.show('Sample Login Error: '+fault.fault.faultString);
			}
		}
	}
}